﻿using Basic.FormCollect;
using Basic.FormCollect.Model;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Form;
using Basic.FormRemoteModel.Form.Model;
using Basic.FormService.Helper;
using Basic.FormService.Interface;
using WeDonekRpc.Client.Interface;

namespace Basic.FormService.RpcEvent
{
    internal class FormColumnEvent : IRpcApiService
    {
        private readonly ICustomFormCollect _Form;
        private readonly IPhysicalColumnCollect _PhyCol;
        private readonly IQueryColumnCollect _QueryCol;
        private readonly ITableColumnCollect _TableColumn;
        private readonly IFormViewColService _ViewCol;
        public FormColumnEvent ( ICustomFormCollect form,
            IPhysicalColumnCollect phyCol,
            IQueryColumnCollect queryCol,
            IFormViewColService viewCol,
            ITableColumnCollect tableColumn )
        {
            this._ViewCol = viewCol;
            this._Form = form;
            this._PhyCol = phyCol;
            this._QueryCol = queryCol;
            this._TableColumn = tableColumn;
        }

        public FormColumn[] GetFormColumns ( GetFormColumns obj )
        {
            FormColGetArg arg = obj.Arg;
            FormType formType = this._Form.Get(arg.FormId, a => a.FormType);
            if ( formType == FormType.代理表单 )
            {
                return this._PhyCol.GetsByTableId(arg.TableId, arg.FuncType, arg.DataType.GetDataType(), a => new FormColumn
                {
                    Id = a.Id,
                    Name = a.Name,
                    Label = a.Label
                });
            }
            else if ( formType == FormType.物理表单 )
            {
                return this._QueryCol.GetsByTableId(arg.TableId, new FormGetParam
                {
                    FuncType = arg.FuncType,
                    DataType = arg.DataType,
                    ShowMode = arg.ShowMode,
                    IsSystem = arg.IsSystem
                }, a => new FormColumn
                {
                    Id = a.Id,
                    Name = a.ColName,
                    Label = a.ColLabel
                });
            }
            else if ( formType == FormType.视图 )
            {
                return this._ViewCol.GetCols(arg.FormId, new FormGetParam
                {
                    FuncType = arg.FuncType,
                    DataType = arg.DataType,
                    ShowMode = arg.ShowMode,
                    IsSystem = arg.IsSystem
                });
            }
            else
            {
                return this._TableColumn.GetsByTableId(arg.TableId, new TableColGetParam
                {
                    DataType = arg.DataType.GetDataType(),
                    ColType = arg.FuncType.ToControlType()
                }, a => new FormColumn
                {
                    Id = a.Id,
                    Label = a.ColTitle,
                    Name = a.ColName
                });
            }
        }
    }
}
